package com.app.megaprosoft.DB;

import java.sql.*;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class CarBand {
	
	 Connect con = new Connect(); 
	 Connection conn = con.Connect();
	 ResultSet rs = null;
	 SQLiteDBClass myDb;
	 SQLiteDatabase db;
	 Context context;
	 
	 private static final String TABLE_CarBands = "_CarBands";
	 public Context getContext() {
		return context;
	 }

	 public void setContext(Context context) {
		this.context = context;
	 }
	 
	public ResultSet selectCarBand() throws Exception {
		String StrSql   = "SELECT	 [OID]," +
									"[IsActive]," +
									"[IsDeleted]," +
									
									"[CarBandCode],"+
									"[CarBandName],"+
									"[CarBandDescription],"+
									"[OrderNo]" +		
                			 
                		  "FROM		 [dbo].[CarBands] " +
                		  
                		  "WHERE	 [IsDeleted]='0' "; 
		
		StrSql += "ORDER BY [CarBandName] ASC ";
		
		return con.selectData(StrSql);
//    	return sqlsel.getReSultSQL(StrSql);
	 }
	
	 public void closeConnecttion() throws Exception {
    	 if (conn != null) {
             conn.close();
         }
    }
	 
	 public void DEFINE_CarBands() {
			rs = null;
			
			myDb = new SQLiteDBClass(getContext());
			db = myDb.getWritableDatabase();
			
			try {
				rs = selectCarBand();
				while (rs.next()) {
					ContentValues Val = new ContentValues();
					Val.put("CarBandsOID", rs.getString("OID"));
					Val.put("CarBandCode", rs.getString("CarBandCode"));
					Val.put("CarBandName", rs.getString("CarBandName"));
					Val.put("CarBandDescription",
							rs.getString("CarBandDescription"));
					if (rs.getString("OrderNo") == null) {
						Val.put("OrderNo", "999");
					} else {
						Val.put("OrderNo", rs.getString("OrderNo"));
					}

					db.insert(TABLE_CarBands, null, Val);
				}
			} catch (Exception e) {
				Log.d("TABLE_CarBands", "Create Table not Successfully.");
			}finally{
				try {
					closeConnecttion();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			Log.d("TABLE_CarBands", "Create Table Successfully.");
			db.close();
		}
	 
	 public String[][] SelectAll_CarBands(String StrOption) throws Exception{
			// TODO Auto-generated method stub
			try {
				String arrData[][] = null;
				
				myDb = new SQLiteDBClass(getContext());
				db = myDb.getWritableDatabase();
				// String strSQL = "SELECT  CarBandsOID As _id , * FROM " +
				// TABLE_CarBands +" ORDER BY OrderNo DESC " ;

				String strSQL = "SELECT  CarBandsOID As _id , * FROM "
						+ TABLE_CarBands;
				if (StrOption.equals("999")) {
					strSQL += " WHERE [OrDerNo] LIKE '" + StrOption + "' ";
				} else {
					strSQL += " WHERE [OrDerNo] NOT LIKE '999' ORDER BY OrDerNo ASC";
				}

				// strSQL += " ORDER BY OrderNo ASC ";
				Cursor cursor = db.rawQuery(strSQL, null);
				if (cursor != null) {
					if (cursor.moveToFirst()) {
						arrData = new String[cursor.getCount()][cursor
								.getColumnCount()];
						int i = 0;
						do {
							arrData[i][0] = cursor.getString(1);
							arrData[i][1] = cursor.getString(2);
							arrData[i][2] = cursor.getString(3);
							arrData[i][3] = cursor.getString(4);
							arrData[i][4] = cursor.getString(5);
							i++;
						} while (cursor.moveToNext());
					}
				}
				cursor.close();
				return arrData;
			} catch (Exception e) {
				return null;
			}
		}
}
